package cn.itcast.service.impl;

import cn.itcast.dao.UserDao;
import cn.itcast.dao.impl.UserDaoImpl;
import cn.itcast.daomain.comment;
import cn.itcast.daomain.commodity;
import cn.itcast.daomain.purchase;
import cn.itcast.daomain.user;
import cn.itcast.service.UserService;
import cn.itcast.util.MailUtils;
import cn.itcast.util.UuidUtil;

import java.util.List;
import java.util.UUID;

public class UserServiceImpl implements UserService {

    UserDao userdao=new UserDaoImpl();

    @Override
    public List<commodity> finAll() {
        return userdao.selec();
    }

    @Override
    public boolean addUser(user user) {

        user username= userdao.addUser(user.getUsername());
        if (username!=null){
            return false;
        }
        String[] id=new String[10002];
        for (int i = 0; i < 10000; i++) {
            //截取最后一部分
            id[i]= UUID.randomUUID().toString().substring(24);
        }

        user.setCode(UuidUtil.getUuid());
        user.setUser_img("img/moren.jpg");
        user.setStatus("N");
        user.setUser_pur(id[611]);
        user.setComm_user(id[0]);
        userdao.save(user);
        String content="<a href='http://localhost:8080/outstanding/activeUserServlet?code="+user.getCode()+"'>点击激活【回头鱼商城】</a>";
        MailUtils.sendMail(user.getUsername(),content,"激活邮件");
        return true;
    }

    @Override
    public boolean active(String code) {
        //1.根据激活码查询用户对象

        user user=userdao.findByCode(code);

        //2.调用dao的修改激活状态的方法
        if (user!=null){
            userdao.updateStatus(user);
            return true;
        }else {
            return false;
        }
    }

    @Override
    public user login(String username, String password) {
        return userdao.login(username,password);
    }

    @Override
    public commodity detail(String id) {
        commodity comm=userdao.details(id);

        return comm;
    }



    @Override
    public int addSop(String foodList, String user, String len, String shou) {

       int han= userdao.addSp(foodList,user,len,shou);
       return han;
    }

    @Override
    public List<commodity> spList(String spName) {

        return userdao.spList(spName);
    }

    @Override
    public int updateUser(String username, String password, String pptPic) {
        return userdao.updateUser(username,password,pptPic);
    }

    @Override
    public user chaxunl(String username) {
        return userdao.chaxunl(username);
    }

    @Override
    public List<purchase> selecPur(String userid) {

        return userdao.selecPur(userid);
    }

    @Override
    public List<comment> comment(String commentId) {
        return userdao.comment_id(commentId);
    }

    @Override
    public int updateshou(String id, String phone) {
        return userdao.updateuserPhoen(id,phone);
    }

    @Override
    public int updatecomment(String tup, String text, String comment, String userid,String purid,String xin) {
        int updatepur = 0;
        user userObject=userdao.userObject(userid);
        if (userObject!=null){
          int addset= userdao.updatecomment(comment,userObject.getAccount(),tup,text,userObject.getUser_img(),xin);
            System.out.println(addset);
          if (addset==1){
             updatepur=userdao.updatepur(purid);
          }else {
              updatepur=0;
          }
        }
        return updatepur;
    }

    @Override
    public int updateUsexiugair(String username, String pptPic) {
         return userdao.updateUser(username,pptPic);
    }


}
